package com.ymatou.iapi.sellertrading.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellertrading.service.ApplyForPostpayCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtOrders;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.database.sqlwapper.YmtOrdersIMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_ApplyForPostpay {
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单申请补款");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new ApplyForPostpayCall().getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyForPostpay_001() {
		Logger.start(true, "卖家申请补款成功");
		try {
			 Logger.comment("前置数据准备");
	         int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(sellerId);
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
	        BigDecimal PostPayAmount = new BigDecimal(40);
	        // 调用无需补款
	        new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
			 // 数据库验证
            DefaultWapper database = new DefaultWapper();
            HashMap<String, Integer> m = new HashMap<String, Integer>();
            m.put("OrderId", orderId);
            m.put("TraddingId", orderId);
            m.put("TradingStatus", 16);
            m.put("SellerId", sellerId);
            Thread.sleep(1000);
            Logger.verifyEquals(1, database.selectOrder(m).size(), "验证订单表记录");
            Logger.comment("校验ymt_orderpostpay表");
            List<Map> orderPostPayList = database.selectOrderPostPayByOrderId(orderId);
            Logger.verifyEquals(1, orderPostPayList.size(), "验证补款记录表");
            Logger.verifyEquals(PostPayAmount,(BigDecimal) orderPostPayList.get(0).get("fAmount"), "验证fAmount");
            Logger.verifyEquals(0, (Integer) orderPostPayList.get(0).get("iAction"), "验证iAction");
            Logger.verifyEquals(1, database.selectTradingStatus(m).size(), "验证交易表记录");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ApplyForPostPay_002() {
		Logger.start(true, "卖家申请补款金额为负值,申请失败");
		try {
			Logger.comment("前置数据准备");
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbyStatus(18);
			int sellerId = list.get(0).getIbuyerid();
			int orderId = list.get(0).getIorderid();
			BigDecimal PostPayAmount = new BigDecimal(-101);
	        Logger.comment("前置数据准备结束");
	        JSONObject   aaJsonObject = new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
			Logger.verifyEquals(201,  aaJsonObject.getInt("Code"), "验证Code");
			Logger.verifyEquals("操作失败",  aaJsonObject.getString("Msg"), "验证Code");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
	 @Test
	  @Category(P1.class)
	@TestCase
	public void Tc_ApplyForPostPay_003() {
		Logger.start(true, "订单不存在，申请补款失败");
		try {
			Logger.comment("前置数据准备");
			int sellerId = 288484;
			int orderId = 1111111;
			BigDecimal PostPayAmount = new BigDecimal(101);
	        Logger.comment("前置数据准备结束");
	        JSONObject   aaJsonObject = new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
			Logger.verifyEquals(201,  aaJsonObject.getInt("Code"), "验证Code");
            Logger.verifyEquals(true, aaJsonObject.getString("Msg").contains("申请补款失败:订单不存在"), "验证Msg");
		} catch (Exception e) {
			 Logger.fail(e);
		}
	}
   
	 @Test
     @Category(P1.class)
     @TestCase
    public void Tc_ApplyForPostPay_004() {
        Logger.start(true, "当前状态不能申请补款");
        try {
        	Logger.comment("前置数据准备");
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbyStatus(18);
			int sellerId = list.get(0).getIbuyerid();
			int orderId = list.get(0).getIorderid();
			BigDecimal PostPayAmount = new BigDecimal(101);
	        Logger.comment("前置数据准备结束");
	        JSONObject   aaJsonObject = new ApplyForPostpayCall(sellerId,sellerId).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
			Logger.verifyEquals(201,  aaJsonObject.getInt("Code"), "验证Code");
            Logger.verifyEquals(true, aaJsonObject.getString("Msg").contains("当前状态不能申请补款"), "验证Msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

	 @Test
     @Category(P1.class)
     @TestCase
    public void Tc_ApplyForPostPay_005() {
        Logger.start(true, "当前订单不属于买手,申请失败");
        try {
        	Logger.comment("前置数据准备");
	         int sellerId = 13212;
			List<YmtOrders> list  = new YmtOrdersIMapper().selectbySellerId(Integer.valueOf(EnvSetup.getData("SellerId")));
			int orderId = list.get(0).getIorderid();
	        Logger.comment("前置数据准备结束");
	        BigDecimal PostPayAmount = new BigDecimal(40);
	        // 调用无需补款
	        JSONObject   aaJsonObject = new ApplyForPostpayCall(13212,13212).callAndGetReturnData(MapUtil.hashMap("OrderId", orderId,"PostPayAmount",PostPayAmount));
			Logger.verifyEquals(201, aaJsonObject.getInt("Code"), "验证Code");
			Logger.verifyIsContains("申请补款失败:用户"+sellerId+"不是订单"+orderId+"的所有者", aaJsonObject.getString("Msg"), "验证msg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
}
